From 0d56e4369b53af2c5960af4827b56b06d9162d62 Mon Sep 17 00:00:00 2001 From: Justin Burkett Date: Thu, 9 Feb 2017 10:27:41 -0500 Subject: [PATCH] Fix handling of key ranges ("a .. d") When the last key in the key sequence is a range, extract the whole range instead of just the final key. Fixes #161 --- which-key-tests.el | 10 +++++++++- which-key.el | 12 ++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/which-key-tests.el b/which-key-tests.el index aa50ec01ae7..1312f832c8d 100644 --- a/which-key-tests.el +++ b/which-key-tests.el @@ -99,7 +99,7 @@ '("SPC t 2" . "[ ] test mode"))))) (ert-deftest which-key-test--maybe-replace-multiple () - "Test `which-key-allow-multiple-replacements'. See #156" + "Test `which-key-allow-multiple-replacements'. See #156." (let ((which-key-replacement-alist '(((nil . "helm") . (nil . "HLM")) ((nil . "projectile") . (nil . "PRJTL")))) @@ -114,5 +114,13 @@ (which-key--maybe-replace '("C-c C-c" . "helm-projectile-x")) '("C-c C-c" . "HLM-PRJTL-x"))))) +(ert-deftest which-key-test--key-extraction () + "Test `which-key--extract-key'. See #161." + (should (equal (which-key--extract-key "SPC a") "a")) + (should (equal (which-key--extract-key "C-x a") "a")) + (should (equal (which-key--extract-key " b a") "a")) + (should (equal (which-key--extract-key " a .. c") "a .. c")) + (should (equal (which-key--extract-key "M-a a .. c") "a .. c"))) + (provide 'which-key-tests) ;;; which-key-tests.el ends here diff --git a/which-key.el b/which-key.el index 4a386a1fd73..e9c2170ebdd 100644 --- a/which-key.el +++ b/which-key.el @@ -1439,6 +1439,14 @@ ORIGINAL-DESCRIPTION is the description given by str)))))) desc)) +(defun which-key--extract-key (key-str) + "Pull the last key (or key range) out of KEY-STR." + (save-match-data + (let ((key-range-regexp "\\`.*\\([^ \t]+ \\.\\. [^ \t]+\\)\\'")) + (if (string-match key-range-regexp key-str) + (match-string 1 key-str) + (car (last (split-string key-str " "))))))) + (defun which-key--format-and-replace (unformatted) "Take a list of (key . desc) cons cells in UNFORMATTED, add faces and perform replacements according to the three replacement @@ -1451,7 +1459,7 @@ alists. Returns a list (key separator description)." (let* ((key (car key-binding)) (orig-desc (cdr key-binding)) (group (which-key--group-p orig-desc)) - (keys (which-key--current-key-string key)) + (keys (concat (which-key--current-key-string) " " key)) (local (eq (which-key--safe-lookup-key local-map (kbd keys)) (intern orig-desc))) (hl-face (which-key--highlight-face orig-desc)) @@ -1459,7 +1467,7 @@ alists. Returns a list (key separator description)." (when (consp key-binding) (push (list (which-key--propertize-key - (car (last (split-string (car key-binding) " ")))) + (which-key--extract-key (car key-binding))) sep-w-face (which-key--propertize-description (cdr key-binding) group local hl-face orig-desc)) -- 2.30.2